package com.rskj.cashbook.vo;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 自动记账规则实体类
 * @author system
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class AutoAccountingRule extends BaseEntity {
    
    /**
     * 规则名称
     */
    private String ruleName;
    
    /**
     * 规则类型：PERIODIC(周期性)、TRIGGER(触发式)、SMART(智能)
     */
    private String ruleType;
    
    /**
     * 记账金额
     */
    private BigDecimal amount;
    
    /**
     * 支付人
     */
    private String payUserName;
    
    /**
     * 受益人
     */
    private String forUserName;
    
    /**
     * 支付方式
     */
    private String payWay;
    
    /**
     * 交易对方
     */
    private String payPlat;
    
    /**
     * 备注
     */
    private String remark;
    
    /**
     * 执行频率：DAILY(每天)、WEEKLY(每周)、MONTHLY(每月)、YEARLY(每年)
     */
    private String frequency;
    
    /**
     * 频率值：如每周几、每月几号等
     */
    private Integer frequencyValue;
    
    /**
     * 规则生效开始日期
     */
    private LocalDate startDate;
    
    /**
     * 规则生效结束日期
     */
    private LocalDate endDate;
    
    /**
     * 工作日限制：如1,2,3,4,5表示周一到周五
     */
    private String weekdays;
    
    /**
     * 月份日期限制：如1,15表示每月1号和15号
     */
    private String monthDays;
    
    /**
     * 排除日期：JSON格式存储排除的日期列表
     */
    private String excludeDates;
    
    /**
     * 是否启用
     */
    private Boolean isActive;
    
    /**
     * 最后执行时间
     */
    private LocalDateTime lastExecuteTime;
    
    /**
     * 下次执行时间
     */
    private LocalDateTime nextExecuteTime;
    
    /**
     * 执行次数
     */
    private Integer executeCount;
    
    /**
     * 规则状态描述
     */
    public String getStatusDescription() {
        if (isActive == null || !isActive) {
            return "已禁用";
        }
        if (endDate != null && LocalDate.now().isAfter(endDate)) {
            return "已过期";
        }
        if (startDate != null && LocalDate.now().isBefore(startDate)) {
            return "未生效";
        }
        return "运行中";
    }
    
    /**
     * 频率描述
     */
    public String getFrequencyDescription() {
        if (frequency == null) return "";
        
        switch (frequency) {
            case "DAILY":
                return "每天";
            case "WEEKLY":
                return "每周" + (frequencyValue != null ? getWeekdayName(frequencyValue) : "");
            case "MONTHLY":
                return "每月" + (frequencyValue != null ? frequencyValue + "号" : "");
            case "YEARLY":
                return "每年";
            default:
                return frequency;
        }
    }
    
    private String getWeekdayName(int weekday) {
        String[] weekdays = {"", "周一", "周二", "周三", "周四", "周五", "周六", "周日"};
        return weekday >= 1 && weekday <= 7 ? weekdays[weekday] : "";
    }
}
